<?php

namespace App\Http\Controllers\Api\Wap\User;

use App\Http\Controllers\Common\ApiController;
use App\Models\Setting\Setting;
use App\Models\User\FlyOrder;
use App\Models\User\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class UserController extends ApiController{


    public function __construct() {
        $this->instance = new User();
    }


    public function submitFlyOrder(Request $request) {
        $flyNumber = $request->input('flyNumber');
        $imgPath = $request->input('imgPath');
        $userId = $request->input('userId');
        $bookingDate = $request->input('bookingDate');

        FlyOrder::query()->updateOrCreate(['fly_number' => $flyNumber,'user_id' => $userId],[
            'fly_number' => $flyNumber,
            'img_path' => $imgPath,
            'user_id' => $userId,
            'booking_date' => $bookingDate,
            'status' => 1
        ]);

        return responseSuccess();
    }

    /**
     * 客服二维码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function clientService(Request $request) {
        $qr = Setting::query()->where('key','qr')->first()->val??'';
        return responseSuccess(compact('qr'));
    }


    public function lst(Request $request) {
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',20);


        $conditions = $request->input();

        $records = $this->instance->getLst($conditions)->paginate($pageSize,$this->getCol,'page',$page);
        return responseSuccess($records->items(),$records->total());

    }

    public function add(Request $request) {
        $info = $request->input();

        $this->validate($request, [

        ],[],[

        ]);
        $record = $this->instance->updateOrCreate(['id'=>$info['id']??0],$info);
        return responseSuccess($record->toArray(),1);
    }

    public function detail(Request $request){
        $id = $request->input('id',0);
        $record = $this->instance->getRecordById($id);
        return responseSuccess($record->toArray(),1);
    }

    public function change(Request $request) {
        $id = $request->input('id',0);
        $status = $request->input('status');
        $this->instance->changeColById($id, compact('status'));
        return responseSuccess();
    }
}
